package com.fan.chat.model;

import com.alibaba.fastjson.JSON;
import com.fan.chat.constant.HTTPConstant;
import com.fan.chat.enums.CusCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class JSONResult<T> {

    private int code;
    private T data;
    private String msg;

    public static <T> JSONResult<T> success() {
        return result(HTTPConstant.JSON_RESULT_SUCCESS_CODE, null, HTTPConstant.JSON_RESULT_SUCCESS_MSG);
    }

    public static <T> JSONResult<T> success(T data) {
        return result(HTTPConstant.JSON_RESULT_SUCCESS_CODE, data, HTTPConstant.JSON_RESULT_SUCCESS_MSG);
    }

    public static <T> JSONResult<T> error() {
        return error(HTTPConstant.JSON_RESULT_ERROR_CODE);
    }

    public static <T> JSONResult<T> error(int code) {
        return result(code, null, HTTPConstant.JSON_RESULT_ERROR_MSG);
    }

    public static <T> JSONResult<T> error(String msg) {
        return error(HTTPConstant.JSON_RESULT_ERROR_CODE, msg);
    }

    public static <T> JSONResult<T> error(int code, String msg) {
        return result(code, null, msg);
    }

    public static <T> JSONResult<T> error(CusCodeEnum codeEnum) {
        return error(codeEnum.getCode(), codeEnum.getMsg());
    }

    public static <T> JSONResult<T> result(int code, T data, String msg) {
        return new JSONResult<T>(code, data, msg);
    }

    public boolean isSuccess() {
        return this.code == HTTPConstant.JSON_RESULT_SUCCESS_CODE;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
